﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LPV_POS.mh_pos.mh_khac
{
    public partial class frmPHIEU_TONKHO_NGUYEN_VAT_LIEU : Form
    {
        public frmPHIEU_TONKHO_NGUYEN_VAT_LIEU()
        {
            InitializeComponent();
        }
        DataTable tb = null;
        private void frmPHIEU_TONKHO_NGUYEN_VAT_LIEU_Load(object sender, EventArgs e)
        {
            cboNam.Items.Add(Variable.ngay_gio_hienhanh.Year + "");
            cboNam.Items.Add((Variable.ngay_gio_hienhanh.Year - 1) + "");
            cboNam.SelectedIndex = 0;

            cboThang.SelectedIndex = Variable.ngay_gio_hienhanh.Month - 1;

            cboKho.DataSource = new thuvien().SP_SelectDataTable("sp_PHIEU_TONKHO_NGUYEN_VAT_LIEU",
                                                    new string[] { "@flag" },
                                                    new object[] { 5 });
            this.bton_Tim_Click(null, null);
        }

        private void bton_Tim_Click(object sender, EventArgs e)
        {
            tb = new thuvien().SP_SelectDataTable("sp_PHIEU_TONKHO_NGUYEN_VAT_LIEU",
                                                                new string[] { "@flag", "@MAKHO", "@nam", "@thang" },
                                                                new object[] { 4, cboKho.SelectedValue, cboNam.Text, cboThang.Text });
            grdPHIEU_TONKHO_NGUYEN_VAT_LIEU.DataSource = tb;
            tb.AcceptChanges();
        }

        private void bton_luu_ctnl_Click(object sender, EventArgs e)
        {
            thuvien othuvien = new thuvien();
            int irt = 0;
            if (tb.GetChanges(DataRowState.Modified) != null)
            {
                foreach (DataRow dr in tb.GetChanges(DataRowState.Modified).Select())
                {
                    irt = othuvien.SP_Update("sp_PHIEU_TONKHO_NGUYEN_VAT_LIEU",
                                        new string[] { "@flag", "@MANGUYENLIEU", "@SOLUONG_TONDAU", 
                                                    "@nam", "@thang", "@MAKHO" },
                                        new object[] { 2, dr["MANGUYENLIEU"], dr["SOLUONG_TONDAU"], 
                                                    cboNam.Text, cboThang.Text, cboKho.SelectedValue });
                }
                if (irt == 1)
                {
                    MessageBox.Show("Lưu thành công");
                    this.bton_Tim_Click(null, null);
                }
            }
            else
                MessageBox.Show("Không có dữ liệu cần lưu","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
    }
}
